//
//  GYMyViewController.m
//  Visitor
//
//  Created by x on 2017/9/15.
//  Copyright © 2017年 HLB. All rights reserved.
//

#import "GYMyViewController.h"
#import "GYMyFirstTableViewCell.h"
#import "GYMySecondTableViewCell.h"
#import "GYMyHeaderView.h"
#import "GYNotDisturbViewController.h"
#import "GYNetWorkTools.h"

#import "NSString+FilePath.h"


@interface GYMyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 表格图片数组 */
@property (nonatomic, strong) NSArray *images;

/** 表格标题数组 */
@property (nonatomic, strong) NSArray *titles;

@end

@implementation GYMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"我的";
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.sectionHeaderHeight = 10;
    
    //去掉导航栏上的下划线
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    GYMyHeaderView *headView = [GYMyHeaderView headerView];
    [headView layoutIfNeeded];
    self.tableView.tableHeaderView = headView;

}
#pragma mark -- <UITableViewDelegate,UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.images.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.images[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 2) {
        GYMyFirstTableViewCell *cell = [GYMyFirstTableViewCell cellWithTableView:tableView];
        cell.imageName = self.images[indexPath.section][indexPath.row];
        cell.title = self.titles[indexPath.section][indexPath.row];
        
        return cell;
    }else {
        GYMySecondTableViewCell *cell = [GYMySecondTableViewCell cellWithTableView:tableView];
        cell.imageName = self.images[indexPath.section][indexPath.row];
        cell.title = self.titles[indexPath.section][indexPath.row];
        cell.indexPath = indexPath;
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        [SVProgressHUD showImage:nil status:@"此功能暂未开启!"];
        switch (indexPath.row) {
            case 0:
            {
//                GYNotDisturbViewController *notDisturbController = [[GYNotDisturbViewController alloc] initWithNibName:@"GYNotDisturbViewController" bundle:nil];
//                [self.navigationController pushViewController:notDisturbController animated:YES];
            }
                break;
                
            default:
                break;
        }
    }else {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"您确定退出吗?" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            //1.首先判断key是否失效
            [GYNetWorkTools loginOut:[[GYUserInfo loadUserInfo] requestKey] susscess:^(NSDictionary *obj) {
                
                //1.删除过期的个人信息
                if (![GYUserInfo removeUserInfo]) {
                    [SVProgressHUD showErrorWithStatus:@"出现一个错误,请重新退出!"];
                }
            } filed:^(NSError *error) {
                [SVProgressHUD showErrorWithStatus:@"退出失败,请检查网络重新退出!"];
                NSLog(@"loginOutFiled========%@",error);
            }];
        }];
        
        [alertController addAction:action];
        [alertController addAction:action1];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
    view.backgroundColor = [UIColor clearColor];
    
    return view;
}
#pragma mark -- 懒加载

- (NSArray *)images
{
    if (!_images) {
        _images = @[@[@"my_icon_dnd",@"my_icon_timing",@"my_icon_news",@"my_icon_we",@"my_icon_cache"],@[@"my_icon_quit"]];
    }
    return _images;
}

- (NSArray *)titles
{
    if (!_titles) {
        _titles = @[@[@"免打扰模式",@"定时开启",@"消息设置",@"关于我们",@"清除缓存"],@[@"退出当前账号"]];;
    }
    return _titles;
}


@end
